package com.jyy.prefabricated.service;

import com.jyy.prefabricated.entity.EligibilityRules;
import com.jyy.prefabricated.entity.ProjectCategory;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.EligibilityRulesMapper;
import com.jyy.prefabricated.mapper.ProjectCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Service
public class ProjectCategoryService {

    @Autowired
    private ProjectCategoryMapper projectCategoryMapper;
    @Resource
    private EligibilityRulesMapper eligibilityRulesDao;

    /**
     * 新增
     * @param projectCategory
     * @return
     */
    public int insert(ProjectCategory projectCategory){
        projectCategoryMapper.insert(projectCategory);
        for (EligibilityRules eligibilityRules:projectCategory.getRules()) {
            eligibilityRules.setCategoryId(projectCategory.getId());
        }
        eligibilityRulesDao.insertBatch(projectCategory.getRules());
        return projectCategory.getId();
    }

    /**
     * 修改
     * @param projectCategory
     * @return
     */
    public int update(ProjectCategory projectCategory){
        if(projectCategory.getId() == null){
            throw new CustomException("参数id不能为空");
        }
        checkCategoryLevel(projectCategory);
        for (EligibilityRules eligibilityRules:projectCategory.getRules()) {
            eligibilityRules.setCategoryId(projectCategory.getId());
        }
        eligibilityRulesDao.deleteByCategoryId(projectCategory.getId());
        eligibilityRulesDao.insertBatch(projectCategory.getRules());
        return projectCategoryMapper.updateByPrimaryKeySelective(projectCategory);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id){

        eligibilityRulesDao.deleteByCategoryId(id);
        return projectCategoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public ProjectCategory selectById(Integer id){
        ProjectCategory projectCategory = projectCategoryMapper.selectByPrimaryKey(id);
        EligibilityRules eligibilityRules = new EligibilityRules();
        eligibilityRules.setCategoryId(id);
        List<EligibilityRules> eligibilityRulesList = eligibilityRulesDao.queryAll(eligibilityRules);
        projectCategory.setRules(eligibilityRulesList);
        return projectCategory;
    }

    /**
     * 查询所有分类
     * @return
     */
    public List<ProjectCategory> selectAll(){
        return projectCategoryMapper.selectAll();
    }

    /**
     * 根据参数查询列表
     * @param category 名称
     * @param pid 父级id
     * @return
     */
    public List<ProjectCategory> selectByParams(String category, Integer pid){
        List<ProjectCategory> projectCategories = projectCategoryMapper.selectByParams(category, pid);
        for (ProjectCategory projectCategory:projectCategories) {
            EligibilityRules eligibilityRules = new EligibilityRules();
            eligibilityRules.setCategoryId(projectCategory.getId());
            List<EligibilityRules> eligibilityRulesList = eligibilityRulesDao.queryAll(eligibilityRules);
            projectCategory.setRules(eligibilityRulesList);
        }
        return projectCategories;
    }

    /**
     * 校验层级
     * @param category
     */
    public void checkCategoryLevel(ProjectCategory category){
        List<ProjectCategory> categories = selectAll();
        List<ProjectCategory> son = findCategoryIdByPid(category.getId(), categories);
        List<Integer> ids = son.stream().map(e->e.getId()).collect(Collectors.toList());
        if(ids.contains(category.getPid()) || category.getId().equals(category.getPid())){
            throw new CustomException("当前分类的父类不能为它的子分类和它本身");
        }
    }

    /**
     * 根据父级id所有子级分类id
     * @param pid
     * @param categories
     * @return
     */
    public List<ProjectCategory> findCategoryIdByPid(Integer pid, List<ProjectCategory> categories){
        List<ProjectCategory> list = new ArrayList<>();
        categories.stream().filter(e->pid.equals(e.getPid())).forEach(item->{
            list.add(item);
            List<ProjectCategory> son = findCategoryIdByPid(item.getId(),categories);
            list.addAll(son);
        });
        return list;
    }
    /**
     * 根据子级id查询父级id
     * id 子级id
     * @return
     */
    public Integer findCategoryIdById(Integer id){
        ProjectCategory projectCategory = projectCategoryMapper.selectByPrimaryKey(id);
        if(projectCategory.getPid() == null){
            return projectCategory.getId();
        }else{
            ProjectCategory fCategory = projectCategoryMapper.selectByPrimaryKey(projectCategory.getPid());
            return findCategoryIdById(fCategory.getId());
        }
    }
}
